import re
import jieba
import codecs
import time
#词典
global DIC
DIC = {}
#最大词的长度
global MAXLEN
MAXLEN = 5
global PRESULT #正向结果
global PNUM #正向结果的单字数量
global RRESULT #逆向结果
global RNUM #逆向结果的单字数量
global RESULT #最终结果

global WORDNUM #字数，包含标点符号
global PTIME #正向的时间
global RTIME #逆向的时间
global ATIME #总时间
global JTIME
PTIME = 0
RTIME = 0
ATIME = 0
JTIME = 0


WORDNUM = 0
#获得分词词典，存储为字典形式
def GetWords():
    f1=open('words/words.txt','r')
    while 1:
        line = f1.readline()
        if not line:
            break
        term = line.strip()
        global DIC
        DIC[term] = 1
        # if len(term)>MAXLEN:
        #     global MAXLEN
        #     MAXLEN = len(term) 15
        # print(term)
    f1.close()
    print("词典转换为dict成功")
#正向最大匹配
def PositiveMatch(str):
    global PRESULT
    global PNUM
    if len(str) == 1:
        PNUM += 1
    if len(str) < 3:
        PRESULT.append(str)
        return
    n = 0
    while n < len(str):
        #是否匹配两个以上的词语
        isMatch = 0
        for i in range(MAXLEN,1,-1):
            ts = str[n:n+i]
            if ts in DIC:
                isMatch = 1
                PRESULT.append(ts)
                n = n+i
                break
        if not isMatch:
            PRESULT.append(str[n])
            n += 1
            PNUM += 1

#逆向最大匹配
def ReverseMatch(str):
    global RRESULT
    global RNUM
    if len(str) == 1:
        RNUM += 1
    if len(str) < 3:
        RRESULT.append(str)
        return
    strLength = len(str)
    while strLength > 0:
        cutLenth = min(strLength,MAXLEN)
        isMatch = 0
        for i in range(cutLenth,1,-1):
            ts = str[strLength-i:strLength]
            if ts in DIC:
                RRESULT.append(ts)
                strLength -= i
                isMatch = 1
                break
        if not isMatch:
            RRESULT.append(str[strLength-1])
            strLength -= 1
    #逆向 反向成为正序
    RRESULT.reverse()
global RWORDNUM #双向匹配词数
global JWORDNUM
global WRONGNUM
RWORDNUM = 0
JWORDNUM = 0
RIGHTNUM = 0
def WordCompile(rlist,jlist):
    global RWORDNUM  # 双向匹配词数
    global JWORDNUM
    global RIGHTNUM
    RWORDNUM += len(rlist)
    JWORDNUM += len(jlist)
    r = 0
    j = 0
    rnum = 0 #zishu
    jnum = 0
    while j < len(jlist) and r < len(rlist):
        # print(r,j)
        if rlist[r] == jlist[j]:
            j += 1
            r += 1
            RIGHTNUM += 1
            continue
        lr = len(rlist[r])
        lj = len(jlist[j])


        rnum += lr
        jnum += lj
        if jnum > rnum:
            jnum -= lj
            r += 1
        elif rnum == jnum:
            r += 1
            j += 1
        else:
            j += 1
            rnum -= lr

        # if lj < lr:
        #     j += 1
        #     wnum -= lj
        #     if abs(wnum) == lj:
        #         wnum = 0
        #         j += 1
        #     continue

#读取带分词文本
def GetTxt(filepath):
    f2 = codecs.open(filepath,'r','utf-8')
    while 1:
        line = f2.readline()
        if not line:
            break
        line = line.strip()
        line = re.split('[，、。：？“”（）":]',line)
        global PRESULT
        global RRESULT
        global RESULT
        RESULT = []
        global PNUM
        global RNUM

        PNUM = 0
        RNUM = 0
        for item in line:
            if item == '':
                continue
            PRESULT = []
            RRESULT = []
            global WORDNUM
            WORDNUM += len(item)

            global PTIME
            global RTIME
            global ATIME
            global JTIME
            ptimes = time.time()
            PositiveMatch(item)
            ptimee = time.time()
            PTIME += ptimee - ptimes

            rtimes = time.time()
            ReverseMatch(item)
            rtimee = time.time()
            RTIME += rtimee - rtimes

            #判断两个list的关系，选择出正确的结果
            #结果相同 任选，结果不一样 选单字数量少，单字相同 选逆向
            atimes = time.time()
            if PRESULT == RRESULT:
                RESULT = PRESULT
            elif PNUM < RNUM:
                RESULT = PRESULT
            else:
                RESULT = RRESULT
            atimee = time.time()
            ATIME += atimee - atimes

            # ATIME += PTIME + RTIME

            print(RESULT)
            jtimes = time.time()
            s = jieba.cut(item)
            jtimee = time.time()
            JTIME += jtimee - jtimes
            s = ' '.join(s).split()
            WordCompile(RRESULT,s)

            print(s)

            # ReverseMatch(item)

    f2.close()

def FileProcessing():
    #数量待修改
    for i in range(0,4):
        filepath = 'testwords/news(' + str(i) + ').txt'
        GetTxt(filepath)